<?php
declare (strict_types = 1);

namespace app\controller\user\v1;

use app\BaseController;
use think\facade\Db;

class Index extends BaseController
{
    /**
     * 获取首页数据
     * @return \think\Response
     */
    public function getHomeData()
    {
        // 获取轮播图
        $banners = Db::table('banner')
            ->where('status', 1)
            ->field('id,imageUrl,linkUrl')
            ->order('sort', 'asc')
            ->select();

        // 获取热门项目
        $workbench = Db::table('workbench')
            ->where('isDelete', 0)
            ->field('id,title,desc,icon,url')
            ->select();

        
        $userInfo = Db::table('wxappUser')
            ->where('uuid', $this->request->uuid)
            ->field('id,points,nickname')
            ->findOrEmpty();

        if(!$userInfo['nickname']){
            //如果为空的话，默认名称是UED.用户ID.用户ID如果小于3位置就给他补充0
            $userInfo['nickname'] = "UED." . str_pad((string)$userInfo['id'], 3, '0', STR_PAD_LEFT);
        }
        //points可能是个正整数也可能是只有一个小数点，也有可能是多个小数点，需要保留两位小数，不够的话需要补0
        $userInfo['points'] = number_format($userInfo['points'], 2, '.', '');

        return result(SUCCESS, '', [
            'banners' => $banners,
            'workbench' => $workbench,
            'userInfo' => $userInfo,
        ]);
    }
}
